package helperClassSites;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import utils.Utils;

public class CreateExplore {

	public static void CreateExploreFiles(String path, int xlsSheet){
		
		try {
			Workbook workbook = Workbook.getWorkbook(new File("C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			Sheet sheet = workbook.getSheet(xlsSheet);
			Cell[] count = sheet.getColumn(0);
			Sheet other = workbook.getSheet(8);
			Cell codeCell = other.getCell(0, 0);
			String code = codeCell.getContents();

			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(9, i);
				String sitename = siteNameCell.getContents();
				File replacedFile = new File(path + sitename + ".html");
				replacedFile.delete();
				FileWriter fstream = new FileWriter(path + sitename + ".html");
				BufferedWriter textFile = new BufferedWriter(fstream);

				textFile.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN''http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html>  \n");
				textFile.write("<head>  \n");
				textFile.write("  <meta http-equiv='content-type' content='text/html; charset=utf-8' />  \n");
		        Cell mountainNameCell = sheet.getCell(4, i);
		        String mountainNameEN = mountainNameCell.getContents();
		        String replaced = Utils.replaceUmlautsDeutsch(mountainNameEN);
				textFile.write("  <meta name='Description' content='Exploring the "+ replaced +" in 3D is amazing. Always wondered what it would feel like to stand on a big mountain in the Alps. You can now explore mountains at Cycling the Alps. There are around 480 paved passes and a lot of mountains on www.cyclingthealps.com. Come and have a look.' /> \n");
				textFile.write("<!-- \n");
				textFile.write("Copyright 2008 Google Inc. \n");
				textFile.write("  \n");
				textFile.write("Licensed under the Apache License, Version 2.0 (the 'License'); \n");
				textFile.write("you may not use this file except in compliance with the License. \n");
				textFile.write("You may obtain a copy of the License at \n");
				textFile.write("  \n");
				textFile.write("     http://www.apache.org/licenses/LICENSE-2.0 \n");
				textFile.write("  \n");
				textFile.write("Unless required by applicable law or agreed to in writing, software \n");
				textFile.write("distributed under the License is distributed on an 'AS IS' BASIS, \n");
				textFile.write("WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. \n");
				textFile.write("See the License for the specific language governing permissions and \n");
				textFile.write("limitations under the License. \n");
				textFile.write("-->  \n");
				textFile.write("  <title>Explore the " + replaced + "</title> \n");
				textFile.write("<script type=\"text/javascript\">var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));</script>");
				textFile.write("    \n");
				textFile.write("<script type=\"text/javascript\">  var _gaq = _gaq || [];  _gaq.push(['_setAccount', 'UA-13084846-1']);  _gaq.push(['_trackPageview']);  (function() {    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  })();</script>");
				textFile.write("    \n");
				textFile.write("  <link rel=stylesheet href='../../explore/lib/firspersoncam.css' type='text/css' /> \n");

				textFile.write("  <script type=\"text/javascript\"> \n");
				textFile.write("  var googleMapsApiKeys = []; \n");
				textFile.write("  googleMapsApiKeys['http://cta.wegewijs.info/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRTBicl7mDxxQ0ZVLXxnkdyYsvjN2xRD0jVY52fdXUpjz9rANIBJiWPV0w';\n");
				textFile.write("  googleMapsApiKeys['http://www.wegewijs.info/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRT-xS-a97Jv2dVLwJNVV1w_TZi6pBTQpY11tZ1Y4fardIpuI2sHtbfrcQ';\n");
				textFile.write("  googleMapsApiKeys['http://www.cyclingthealps.com/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRRu0hKdmJtwqUpy0N8zchdihiwGgRT7yprgzTDNAnphI0d4JwZ71whrhA';\n");
				textFile.write("  googleMapsApiKeys['http://cta.cyclingthealps.com/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRRxWkNdpiRta-ntjL1r0v29A2_XuBQriWGZ66izn5yupLMismdpENe4MA';\n");
				textFile.write("  googleMapsApiKeys['http://alpspasses.appspot.com/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRTn0IpzAJXD01_fqfQFNim4xN6saxRtEL98s25b9oDrAP9cOhoAO2sfMQ';\n");
				textFile.write(code + " \n");
				textFile.write(" </script>\n");

				// textFile.write("  <script src=\"http://www.google.com/jsapi?key=ABQIAAAAM4pWLS64lWH2T51CW5FpkRRu0hKdmJtwqUpy0N8zchdihiwGgRT7yprgzTDNAnphI0d4JwZ71whrhA\"></script> \n");
				textFile.write("  <script type=\"text/javascript\" src=\"../../explore/lib/math3d.js\"></script> \n");
				textFile.write("  <script type=\"text/javascript\" src=\"../../explore/lib/keyboard-focus-hack.js\"></script> \n");
				textFile.write("  <script type=\"text/javascript\" src=\"../../explore/lib/firstpersoncam.js\"></script> \n");
				textFile.write("  <script type=\"text/javascript\"> \n");
				textFile.write("google.load('earth', '1'); \n");
				textFile.write("google.load('maps', '2'); \n");
				textFile.write("  \n");
				textFile.write("var ge = null; \n");
				textFile.write("var cam; \n");
				textFile.write("  \n");
				Cell latlngCellX = sheet.getCell(1, i);
				String latlngX = latlngCellX.getContents();
				Cell latlngCellY = sheet.getCell(2, i);
				String latlngY = latlngCellY.getContents();
				textFile.write("var lat = " + latlngX + ";\n");
				textFile.write("var lon = " + latlngY + ";\n");
				Cell headingCellX = sheet.getCell(26, i);
				String heading = headingCellX.getContents();
				textFile.write("var heading = " + heading +";\n");
				Cell linkCell = sheet.getCell(16, i);
				String link = linkCell.getContents();
				textFile.write("var link = '" + link + "';\n");
				textFile.write("  \n");
				textFile.write("function init() { \n");
				textFile.write("  init3D(); \n");
				textFile.write("} \n");
				textFile.write("  \n");
				textFile.write("function initCB(object) { \n");
				textFile.write("  ge = object; \n");
				textFile.write("  ge.getOptions().setFlyToSpeed(100); \n");
				textFile.write("  \n");
				textFile.write("  ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true); \n");
				textFile.write("  \n");
				textFile.write("  cam = new FirstPersonCam(lat, lon, heading, link); \n");
				textFile.write("  cam.updateCamera(); \n");
				textFile.write("  ge.getWindow().setVisibility(true); \n");
				textFile.write("  \n");
				textFile.write("  keyboardFocusHack(ge); \n");
				textFile.write("} \n");
				textFile.write("  \n");
				textFile.write("function failureCB(object) { \n");
				textFile.write("  /*** \n");
				textFile.write("   * This function will be called if plugin fails to load, in case \n");
				textFile.write("   * you need to handle that error condition. \n");
				textFile.write("   ***/ \n");
				textFile.write("} \n");
				textFile.write("  \n");
				textFile.write("function init3D() { \n");
				textFile.write("  google.earth.createInstance('map3d', initCB, failureCB); \n");
				textFile.write("} \n");
				textFile.write("  \n");
				textFile.write("  </script>  \n");
				textFile.write("</head>  \n");
				textFile.write("<body onload='init()' onunload='GUnload()' onKeyDown='return keyDown(event);' onKeyUp='return keyUp(event);' id='body' style= \"padding: 0; overflow: hidden; margin: 0; background: #fafad7;\">  \n");
//				textFile.write("  <center> \n");
//				textFile.write("	    Explore the Alps " + replaced + "  <INPUT TYPE=\"button\"  VALUE=\"Close\" ONCLICK=\"window.location.href='http://www.cyclingthealps.com'\">\n");
//				textFile.write("   \n");
//				textFile.write("  </center> \n");
//				textFile.write("    <table border=0>  \n");
//				textFile.write("  <tr>  \n");
//				textFile.write("    <td valign=top>  \n");
//				textFile.write("	<br> \n");
//				textFile.write("    Movement:<br>  \n");
//				textFile.write("    &nbsp;&nbsp;W = Forwards<br>  \n");
//				textFile.write("    &nbsp;&nbsp;S = Backwards<br>  \n");
//				textFile.write("    &nbsp;&nbsp;A = Strafe Left<br>  \n");
//				textFile.write("    &nbsp;&nbsp;D = Strafe Right<br>  \n");
//				textFile.write("    &nbsp;&nbsp;Page Up = Move Up<br>  \n");
//				textFile.write("    &nbsp;&nbsp;Page Down = Move Down<br>  \n");
//				textFile.write("    <br>  \n");
//				textFile.write("    Camera View:<br>  \n");
//				textFile.write("    &nbsp;&nbsp;Up Arrow = Tilt up<br>  \n");
//				textFile.write("    &nbsp;&nbsp;Down Arrow = Tilt down<br>  \n");
//				textFile.write("    &nbsp;&nbsp;Right Arrow = Turn right<br>  \n");
//				textFile.write("    &nbsp;&nbsp;Left Arrow = Turn left<br>   \n");
//				textFile.write("    </td>  \n");
//				textFile.write("	  	<td valign=top>  \n");
//				textFile.write("      <div id='map3d' style='position:absolute;;border:0px;width:87%;height:96%;;'></div> \n");
//				textFile.write("    </td>  \n");
//				textFile.write("  </tr>  \n");
//				textFile.write("  </table>   \n");
				textFile.write("  <div align=\"right\">\n");
				Cell hyperlinkNameCell = sheet.getCell(35, i);
				String hyperlink = hyperlinkNameCell.getContents();
				textFile.write(" <a href=\"http://www.cyclingthealps.com/#!" + hyperlink +"\"> <img src=\"http://www.cyclingthealps.com/images/close.png\"> </a>\n");
				textFile.write("</div>\n"); 
				textFile.write("<center> \n");
				textFile.write("  <div id=\"map3d\" style=\"position:absolute;;border:0px;width:100%;height:97%;;\"></div>\n");
				textFile.write("</center> \n");
				textFile.write("</body>  \n");
				textFile.write("</html>  \n");
				textFile.close();
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
